<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/12/30
 * Time: 8:34
 */
namespace app\wxapi\controller;

use think\Db;
use think\Request;
use think\Controller;
class login extends Nocommon
{
    /*
     * 进行账号选择
     */
    public function login()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            //景点核销
            if($post['code']==1){
                //检查手机号是否存在
                $checkTel = Db::name('tour_admin')->where("t_admin_tel", $post['phone'])->find();
                if ($checkTel) {
                    //检查密码是否正确
                    $hasher = new \PasswordHash(8, false);
                    if ($hasher->CheckPassword($post['password'], $checkTel['password'])) {
                        //生成token
                        $user_token = md5(uniqid(md5(microtime(true)), true));
                        //将用户token更新
                        Db::name('tour_admin')->where("t_admin_id", $checkTel['t_admin_id'])->setField('token', $user_token);
                        $this->apiReturn('200', '登录成功', array('token' => $user_token, 'admin_id' => $checkTel['t_admin_id'], 'id' => $checkTel['tour_id'],'code'=>1));
                    } else {
                        $this->apiReturn('401', '密码错误');
                    }
                } else {
                    $this->apiReturn('403', '账户不存在');
                }
            }else if($post['code']==2){ //酒店核销
                //检查手机号是否存在
                $checkTel = Db::name('hotel_admin')->where("h_admin_name", $post['phone'])->find();
                if ($checkTel) {
                    //检查密码是否正确
                    $hasher = new \PasswordHash(8, false);
                    if ($hasher->CheckPassword($post['password'], $checkTel['password'])) {
                        //生成token
                        $user_token = md5(uniqid(md5(microtime(true)), true));
                        //将用户token更新
                        Db::name('hotel_admin')->where("h_admin_id", $checkTel['h_admin_id'])->setField('token', $user_token);
                        $this->apiReturn('200', '登录成功', array('token' => $user_token, 'admin_id' => $checkTel['h_admin_id'], 'id' => $checkTel['hotel_id'],'code'=>2));
                    } else {
                        $this->apiReturn('401', '密码错误');
                    }
                } else {
                    $this->apiReturn('403', '账户不存在');
                }
            }else if($post['code']==3){
                //检查手机号是否存在
                $checkTel = Db::name('team_admin')->where("t_admin_tel", $post['phone'])->find();
                if ($checkTel) {
                    //检查密码是否正确
                    $hasher = new \PasswordHash(8, false);
                    if ($hasher->CheckPassword($post['password'], $checkTel['password'])) {
                        //生成token
                        $user_token = md5(uniqid(md5(microtime(true)), true));
                        //将用户token更新
                        Db::name('team_admin')->where("t_admin_id", $checkTel['t_admin_id'])->setField('token', $user_token);
                        $this->apiReturn('200', '登录成功', array('token' => $user_token, 'admin_id' => $checkTel['t_admin_id'], 'id' => $checkTel['team_id'],'code'=>3));
                    } else {
                        $this->apiReturn('401', '密码错误');
                    }
                } else {
                    $this->apiReturn('403', '账户不存在');
                }
            }
        }
    }

    /*
     * 修改景点密码
     */
    public function tour_edit_password()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            //加密方式
            $hasher = new \PasswordHash(8, false);
            //查询当前密码是否正常
            $checkTel = Db::name('tour_admin')->where('t_admin_id',$post['admin_id'])->find();
            if($checkTel){
                if (!$hasher->CheckPassword($post['login_password'], $checkTel['password'])) {
                    $this->apiReturn('400', '当前登录密码错误');
                }
            }
            //判断新密码和确认密码是否输入一致
            if($post['new_password'] != $post['re_password'] ){
                $this->apiReturn('400','新密码和旧密码不一致');
            }
            //加密密码
            $password = $hasher->HashPassword($post['new_password']);

            $data = [
                'password'=>$password
            ];

            $update = Db::name('tour_admin')->where('t_admin_id',$post['admin_id'])->update($data);
            if($update){
                $this->apiReturn('200','修改密码成功');
            }else{
                $this->apiReturn('400','修改密码失败');
            }

        }
    }
    /*
     * 修改酒店密码
     */
    public function hotel_edit_password()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            //加密方式
            $hasher = new \PasswordHash(8, false);
            //查询当前密码是否正常
            $checkTel = Db::name('hotel_admin')->where('h_admin_id',$post['admin_id'])->find();
            if($checkTel){
                if (!$hasher->CheckPassword($post['login_password'], $checkTel['password'])) {
                    $this->apiReturn('400', '当前登录密码错误');
                }
            }
            //判断新密码和确认密码是否输入一致
            if($post['new_password'] != $post['re_password'] ){
                $this->apiReturn('400','新密码和旧密码不一致');
            }
            //加密密码
            $password = $hasher->HashPassword($post['new_password']);

            $data = [
                'password'=>$password
            ];

            $update = Db::name('hotel_admin')->where('h_admin_id',$post['admin_id'])->update($data);
            if($update){
                $this->apiReturn('200','修改密码成功');
            }else{
                $this->apiReturn('400','修改密码失败');
            }

        }
    }

    /*
     * 修改旅行社密码
     */
    public function team_edit_password()
    {
        if (Request::instance()->isPost()) {
            $post = Request::instance()->post();
            //加密方式
            $hasher = new \PasswordHash(8, false);
            //查询当前密码是否正常
            $checkTel = Db::name('team_admin')->where('t_admin_id', $post['admin_id'])->find();
            if ($checkTel) {
                if (!$hasher->CheckPassword($post['login_password'], $checkTel['password'])) {
                    $this->apiReturn('400', '当前登录密码错误');
                }
            }
            //判断新密码和确认密码是否输入一致
            if ($post['new_password'] != $post['re_password']) {
                $this->apiReturn('400', '新密码和旧密码不一致');
            }
            //加密密码
            $password = $hasher->HashPassword($post['new_password']);

            $data = [
                'password' => $password
            ];

            $update = Db::name('team_admin')->where('t_admin_id', $post['admin_id'])->update($data);
            if ($update) {
                $this->apiReturn('200', '修改密码成功');
            } else {
                $this->apiReturn('400', '修改密码失败');
            }

        }
    }
}